/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumjdbc;

import java.sql.Time;
import java.util.Calendar;

import fr.x9c.cadmium.kernel.Block;
import fr.x9c.cadmium.kernel.CodeRunner;
import fr.x9c.cadmium.kernel.Custom;
import fr.x9c.cadmium.kernel.Fail;
import fr.x9c.cadmium.kernel.Primitive;
import fr.x9c.cadmium.kernel.PrimitiveProvider;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class provides the primitives related to the <tt>java.sql.Time</tt> class.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
@PrimitiveProvider
public final class Times {

    /**
     * No instance of this class.
     */
    private Times() {
    } // end empty constructor

    /**
     * Creates a time instance corresponding to the current time.
     * @param ctxt context
     * @param unit ignored
     * @return a new time instance
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_time_make(final CodeRunner ctxt,
                                              final Value unit)
        throws Fail.Exception {
        try {
            return Cadmium.createObject(new Time(Calendar.getInstance().getTimeInMillis()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_time_make(CodeRunner, Value)'

    /**
     * Creates a time instance from an integer value.
     * @param ctxt context
     * @param v the number of milliseconds since the epoch
     * @return a new time instance
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_time_make_of_int64(final CodeRunner ctxt,
                                                       final Value v)
        throws Fail.Exception {
        try {
            return Cadmium.createObject(new Time(v.asBlock().asInt64()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_time_make_of_int64(CodeRunner, Value)'

    /**
     * Creates a time instance from a string value.
     * @param ctxt context
     * @param v the time as a string
     * @return a new time instance
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_time_make_of_string(final CodeRunner ctxt,
                                                        final Value v)
        throws Fail.Exception {
        try {
            return Cadmium.createObject(Time.valueOf(v.asBlock().asString()));
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_time_make_of_string(CodeRunner, Value)'

    /**
     * Returns the number of milliseconds since the epoch.
     * @param ctxt context
     * @param time time
     * @return the number of milliseconds since the epoch
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_time_get_time(final CodeRunner ctxt,
                                                  final Value time)
        throws Fail.Exception {
        try {
            final Time t = (Time) time.asBlock().asCustom();
            final Block res = Block.createCustom(Custom.INT_64_SIZE,
                                                 Custom.INT_64_OPS);
            res.setInt64(t.getTime());
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_time_get_time(CodeRunner, Value)'

    /**
     * Changes the number of milliseconds since the epoch.
     * @param ctxt context
     * @param time time
     * @param v the number of milliseconds since the epoch
     * @return <i>unit</i>
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_time_set_time(final CodeRunner ctxt,
                                                  final Value time,
                                                  final Value v)
        throws Fail.Exception {
        try {
            final Time t = (Time) time.asBlock().asCustom();
            t.setTime(v.asBlock().asInt64());
            return Value.UNIT;
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method 'cadmiumjdbc_time_set_time(CodeRunner, Value, Value)'

    /**
     * Converts a time into a string.
     * @param ctxt context
     * @param time time
     * @return a string corresponding to the passed time
     * @throws Fail.Exception if any error occurs
     */
    @Primitive
    public static Value cadmiumjdbc_time_to_string(final CodeRunner ctxt,
                                                   final Value time)
        throws Fail.Exception {
        try {
            final Time t = (Time) time.asBlock().asCustom();
            final Block res = Block.createString(t.toString());
            return Value.createFromBlock(res);
        } catch (final Exception e) {
            Cadmium.fail(ctxt, e);
            return Value.UNIT; // never reached
        } // end try/catch
    } // end method ' cadmiumjdbc_time_to_string(CodeRunner, Value)'

} // end class 'Times'
